from django.shortcuts import render
from django.template.exceptions import TemplateDoesNotExist
from django.utils import translation
from django.views.decorators.csrf import ensure_csrf_cookie
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.core.handlers.wsgi import WSGIRequest
from django.shortcuts import render, redirect
from django.utils.translation import ugettext as _
from django.urls import reverse

from .models.users import User, AccountAction
from hte.error.handle import error_page


# Create your views here.
@ensure_csrf_cookie
def login_view(request: WSGIRequest):
    next_page = request.GET.get('next', reverse('site_index'))
    if not request.user.is_authenticated:
        return render(request, template_name='accounts/login.html')
    try:
        return redirect(next_page)
    except Exception:
        return redirect(reverse('site_index'))


@ensure_csrf_cookie
def register_view(request: WSGIRequest):
    if request.user.is_authenticated:
        return redirect(reverse('site_index'))
    else:
        return render(request, template_name='accounts/register.html')


def logout_view(request: WSGIRequest):
    next_page = request.GET.get('next', reverse('next', reverse('site_index')))
    if request.user.is_authenticated:
        logout(request)
    try:
        return redirect(next_page)
    except Exception:
        return redirect(reverse('site_index'))


@login_required
def notifications(request: WSGIRequest):
    return render(request, template_name='accounts/my_info.html', context={'notifications': _('YES!')})


@login_required
def my_info(request: WSGIRequest):
    return render(request, template_name='accounts/my_info.html')


def about(request: WSGIRequest):
    # return render(request, template_name='accounts/about.html')
    lang = translation.get_language()
    try:
        return render(request, 'accounts/about_%s.html' % lang[:2])
    except TemplateDoesNotExist:
        return render(request, 'accounts/about_zh.html')


@login_required
def my_data(request: WSGIRequest):
    return render(request, template_name='accounts/my_sources_data.html')


@login_required
def my_task(request: WSGIRequest):
    return render(request, template_name='accounts/my_sources_task.html')


@ensure_csrf_cookie
def reset_password_request(request: WSGIRequest):
    '''
    登录界面点击忘记密码之后出现的页面
    :param request: 
    :return: 
    '''
    return render(request, template_name='accounts/reset_password_request.html')


@ensure_csrf_cookie
def reset_password(request: WSGIRequest):
    error = error_page(message=_('Invalid link!'),
                       detail=_('The link is invalid or your email address is already verified.'))
    token = request.GET.get('token', None)
    if not token:
        return error_page(message=_('Invalid link!'))
    user = User.user_from_token(token)
    if not user or not user.verify_token(token, AccountAction.RESET_PASSWORD):
        return error
    return render(request, template_name='accounts/reset_password.html')


def verify_email(request: WSGIRequest):
    error = error_page(message=_('Invalid link!'),
                       detail=_('The link is invalid or your email address is already verified.'))
    token = request.GET.get('token', None)
    if not token:
        return error
    user = User.user_from_token(token)
    if not user or not user.verify_token(token, AccountAction.VERIFY_EMAIL):
        return error
    return render(request, template_name='success.html',
                  context={'title': _('Success!'), 'message': _('Your email address is verified.')})
